<template>
  <div class="app-container">
    <el-tabs v-model="activeTab">
      <el-tab-pane label="参保人员信息" name="info">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="姓名" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="身份证号" prop="idCard">
            <el-input
              v-model="queryParams.idCard"
              placeholder="请输入身份证号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="personList">
          <el-table-column label="序号" align="center" type="id" width="50" />
          <el-table-column label="姓名" align="center" prop="name" />
          <el-table-column label="身份证号" align="center" prop="idCard" :show-overflow-tooltip="true" />
          <el-table-column label="性别" align="center" prop="gender">
            <template slot-scope="scope">
              {{ scope.row.gender === 0 ? '男' : '女' }}
            </template>
          </el-table-column>
          <el-table-column label="年龄" align="center" prop="age" />
          <el-table-column label="联系电话" align="center" prop="phone" />
          <el-table-column label="参保日期" align="center" prop="insuranceDate" />
          <el-table-column label="个人账户余额" align="center" prop="personalBalance" />
          <el-table-column label="基金账户余额" align="center" prop="fundBalance" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleViewDetail(scope.row)"
              >查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>

      <el-tab-pane label="报销记录" name="record">
        <el-form :model="recordParams" ref="recordForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="参保人" prop="name">
            <el-input
              v-model="recordParams.name"
              placeholder="请输入参保人姓名"
              clearable
              @keyup.enter.native="handleRecordQuery"
            />
          </el-form-item>
<!--          <el-form-item label="时间范围">-->
<!--            <el-date-picker-->
<!--              v-model="dateRange"-->
<!--              type="daterange"-->
<!--              range-separator="至"-->
<!--              start-placeholder="开始日期"-->
<!--              end-placeholder="结束日期"-->
<!--              value-format="yyyy-MM-dd"-->
<!--              @change="handleRecordQuery"-->
<!--            ></el-date-picker>-->
<!--          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleRecordQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetRecordQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loadingRecord" :data="recordList">
          <el-table-column label="序号" align="center" type="id" width="50" />
          <el-table-column label="参保人" align="center" prop="personName" />
          <el-table-column label="医疗机构" align="center" prop="medicalInstitution" />
          <el-table-column label="就诊日期" align="center" prop="visitDate" />
          <el-table-column label="医疗总费用" align="center" prop="totalMedicalCost" />
          <el-table-column label="自付金额" align="center" prop="selfPayment" />
          <el-table-column label="报销金额" align="center" prop="reimbursementAmount" />
          <el-table-column label="转入账户" align="center" prop="transferAccount" />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <el-tag type="warning" v-if="scope.row.status === 0">处理中</el-tag>
              <el-tag type="success" v-else-if="scope.row.status === 1">已报销</el-tag>
              <el-tag type="danger" v-else-if="scope.row.status === 2">已拒绝</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleViewRecord(scope.row)"
              >查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="recordTotal>0"
          :total="recordTotal"
          :page.sync="recordParams.pageNum"
          :limit.sync="recordParams.pageSize"
          @pagination="getRecordList"
        />
      </el-tab-pane>
    </el-tabs>

    <!-- 参保人详情对话框 -->
    <el-dialog title="参保人详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-descriptions title="基本信息" :column="1" border>
            <el-descriptions-item label="姓名">{{ detail.name }}</el-descriptions-item>
            <el-descriptions-item label="身份证号">{{ detail.idCard }}</el-descriptions-item>
            <el-descriptions-item label="性别">{{ detail.gender === 0 ? '男' : '女' }}</el-descriptions-item>
            <el-descriptions-item label="年龄">{{ detail.age }}</el-descriptions-item>
            <el-descriptions-item label="联系电话">{{ detail.phone }}</el-descriptions-item>
            <el-descriptions-item label="参保日期">{{ detail.insuranceDate }}</el-descriptions-item>
            <el-descriptions-item label="参保类型">{{ detail.insuranceType }}</el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="12">
          <el-card class="account-card personal">
            <div slot="header" class="clearfix">
              <span>个人账户</span>
            </div>
            <div class="account-balance">
              <div class="balance-label">当前余额</div>
              <div class="balance-value">{{ detail.personalBalance }} 元</div>
            </div>
            <div class="account-info">
              <div>账户编号：{{ detail.personalCode }}</div>
<!--              <div>最后更新：{{ detail.personalAccountUpdateTime }}</div>-->
            </div>
          </el-card>
          <el-card class="account-card fund">
            <div slot="header" class="clearfix">
              <span>基金账户</span>
            </div>
            <div class="account-balance">
              <div class="balance-label">当前余额</div>
              <div class="balance-value">{{ detail.fundBalance }} 元</div>
            </div>
            <div class="account-info">
              <div>账户编号：{{ detail.fundCode }}</div>
<!--              <div>最后更新：{{ detail.fundAccountUpdateTime }}</div>-->
            </div>
          </el-card>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 报销记录详情对话框 -->
    <el-dialog title="报销记录详情" :visible.sync="recordDetailOpen" width="700px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="参保人">{{ recordDetail.personName }}</el-descriptions-item>
        <el-descriptions-item label="身份证号">{{ recordDetail.personIdCard }}</el-descriptions-item>
        <el-descriptions-item label="医疗机构">{{ recordDetail.medicalInstitution }}</el-descriptions-item>
        <el-descriptions-item label="就诊日期">{{ recordDetail.visitDate }}</el-descriptions-item>
        <el-descriptions-item label="医疗总费用">{{ recordDetail.totalMedicalCost }} 元</el-descriptions-item>
        <el-descriptions-item label="自付金额">{{ recordDetail.selfPayment }} 元</el-descriptions-item>
        <el-descriptions-item label="报销金额">{{ recordDetail.reimbursementAmount }} 元</el-descriptions-item>
        <el-descriptions-item label="转入账户">{{ recordDetail.transferAccount }}</el-descriptions-item>
<!--        <el-descriptions-item label="转入账户">-->
<!--          <el-tag type="primary">参保人账户</el-tag>-->
<!--&lt;!&ndash;          <el-tag type="primary" v-if="recordDetail.accountType === '1'">个人账户</el-tag>&ndash;&gt;-->
<!--&lt;!&ndash;          <el-tag type="success" v-else-if="recordDetail.accountType === '2'">基金账户</el-tag>&ndash;&gt;-->
<!--        </el-descriptions-item>-->
        <el-descriptions-item label="转账时间" :span="2">{{ recordDetail.createTime }}</el-descriptions-item>
        <el-descriptions-item label="诊断信息" :span="2">{{ recordDetail.visitDate }}</el-descriptions-item>
        <el-descriptions-item label="状态" :span="2">
          <el-tag type="warning" v-if="recordDetail.status === 0">处理中</el-tag>
          <el-tag type="success" v-else-if="recordDetail.status === 1">已报销</el-tag>
          <el-tag type="danger" v-else-if="recordDetail.status === 2 ">已拒绝</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="备注信息" :span="2">{{ recordDetail.remark }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="recordDetailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listInsuredPersons} from "@/api/medical/insuredperson";
import {listMedicalReimbursement} from "@/api/medical/medicalunit";
export default {
  name: "InsuredPerson",
  data() {
    return {
      // 当前激活的选项卡
      activeTab: 'info',
      // 遮罩层
      loading: false,
      loadingRecord: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      recordTotal: 0,
      // 参保人员列表
      personList: [],
      // 报销记录列表
      recordList: [],
      // 日期范围
      dateRange: [],
      // 详情对话框
      detailOpen: false,
      // 报销记录详情对话框
      recordDetailOpen: false,
      // 详情数据
      detail: {},
      // 报销记录详情
      recordDetail: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        idCard: undefined
      },
      // 报销记录查询参数
      recordParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      }
    };
  },
  created() {
    this.loading = false;
    this.getList();
    this.getRecordList();
  },
  methods: {
    /** 查询参保人员列表 */
    async getList() {
      this.loading = true;
      const res = await listInsuredPersons(this.queryParams)
      // console.log(res)
      this.personList = res.rows.map(item => {
        return {
          id: item.id,
          age: item.age,
          fundBalance: item.fundBalance,
          fundCode: item.fundCode || '',
          gender: item.gender || 0,
          idCard: item.idCard ||'',

          insuranceDate: item.insuranceDate || '',
          insuranceType: item.insuranceType || '',
          name: item.name || '',
          personalBalance: item.personalBalance,
          personalCode: item.personalCode || '',
          phone: item.phone || '',
          remark: item.remark || '',
        }
      }) || []

      this.total = this.personList.length;
      this.loading = false;
    },
    /** 查询报销记录列表 */
    async getRecordList() {
      this.loadingRecord = true;
      const res = await listMedicalReimbursement(this.recordParams)
      console.log(res)
      this.recordList = res.rows.map(item => {
        return {
          id: item.id,
          medicalInstitution: item.medicalInstitution || '',
          insuredPersonD: item.insuredPersonD,
          insuredPersons: item.insuredPersons || {},
          personName:item.insuredPersons.name,
          personIdCard:item.insuredPersons.idCard,
          reimbursementAmount: item.reimbursementAmount || 0.0,
          remark: item.remark ||'',

          selfPayment: item.selfPayment || 0.0,
          status: item.status || 0,
          totalMedicalCost: item.totalMedicalCost || 0.0,
          transferAccount: item.transferAccount || '',
          visitDate: item.visitDate || '',
          createTime: item.createTime || ''
        }
      }) || [];
      this.recordTotal = this.recordList.length;
      this.loadingRecord = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索报销记录按钮操作 */
    handleRecordQuery() {
      this.recordParams.pageNum = 1;
      this.getRecordList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 重置报销记录查询按钮操作 */
    resetRecordQuery() {
      this.resetForm("recordForm");
      this.dateRange = [];
      this.handleRecordQuery();
    },
    /** 查看参保人详情按钮操作 */
    handleViewDetail(row) {
      this.detail = row;
      this.detailOpen = true;
    },
    /** 查看报销记录详情按钮操作 */
    handleViewRecord(row) {
      this.recordDetail = row;
      this.recordDetailOpen = true;
    }
  }
};
</script>

<style scoped>
.account-card {
  margin-bottom: 20px;
}
.account-card.personal {
  background-color: #f0f9ff;
}
.account-card.fund {
  background-color: #f0f9eb;
}
.account-balance {
  text-align: center;
  padding: 10px 0;
}
.balance-label {
  font-size: 14px;
  color: #606266;
}
.balance-value {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
  margin-top: 5px;
}
.account-card.fund .balance-value {
  color: #67C23A;
}
.account-info {
  font-size: 12px;
  color: #909399;
  margin-top: 10px;
}
.account-info div {
  margin-bottom: 5px;
}
</style>
